home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Text⁄Files / FaceLift / FaceLift Folder / FLMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  7.9 KB  |  477 lines  |  [TEXT/KAHL]

  1. # include    "TransSkel.h"
  2.  
  3. # include    "FLMaca.h"
  4. # include    "FaceLift.h"
  5.  
  6.  
  7. typedef enum        /* File menu item numbers */
  8. {
  9.     newMap = 1,
  10.     openMap,
  11.     appendMap,
  12.     saveMap,
  13.     saveMapAs,
  14.     close,
  15.     /* --- */
  16.     reformat = 8,
  17.     reformatAs,
  18.     /* --- */
  19.     quit = 11
  20. };
  21.  
  22.  
  23. typedef enum        /* Edit menu item numbers */
  24. {
  25.     undo = 1,
  26.     /* --- */
  27.     cut = 3,
  28.     copy,
  29.     paste,
  30.     clear,
  31.     /* --- */
  32.     new = 8,
  33.     duplicate,
  34.     /* --- */
  35.     sort = 11,
  36.     squish,
  37.     reverse
  38. };
  39.  
  40.  
  41. typedef enum            /* Options menu items */
  42. {
  43.     showFormats = 1,
  44.     useFormats,
  45.     addFormats,
  46.     /* --- */
  47.     showBad = 5,
  48.     /* --- */
  49.     stdFontList = 7,
  50.     systemFontList,
  51.     /* --- */
  52.     showFont = 10,
  53.     /* --- */
  54.     getInfo = 12
  55. };
  56.  
  57.  
  58. static MenuHandle    fileMenu;
  59. static MenuHandle    editMenu;
  60. static MenuHandle    optionsMenu;
  61.  
  62. static MapSpec        clipMSpec;                /* clipboard specification */
  63. static Boolean        haveClipSpec = false;    /* can't paste until cut/copy */
  64.  
  65.  
  66. /*
  67.  * Enable or disable menu or menu items, according to the string.
  68.  * First char of string is for entire menu, following chars are
  69.  * for successive items.  '0' disables, '1' enables.
  70.  *
  71.  * Return true if the status of the menu itself changed
  72.  * value, i.e., if the menu bar now needs redrawing.
  73.  */
  74.  
  75. static Boolean
  76. SetMenuStatus (m, s)
  77. MenuHandle    m;
  78. StringPtr    s;
  79. {
  80. int    i;
  81. int    menuState;
  82.  
  83.     menuState = ((**m).enableFlags & 1);    /* current state of menu */
  84.     for (i = 1; i <= s[0]; ++i)
  85.     {
  86.         if (s[i] == '0')
  87.             DisableItem (m, i - 1);
  88.         else
  89.             EnableItem (m, i - 1);
  90.     }
  91.     return (menuState != ((**m).enableFlags & 1));
  92. }
  93.  
  94.  
  95. /*
  96.  * Fix up menus to match window states.  There will always be some
  97.  * window.
  98.  *
  99.  * Note the handling of strings.  Those that may change need to be
  100.  * CopyString'ed into the string, otherwise the string constant
  101.  * itself will be changed.
  102.  */
  103.  
  104. void
  105. FixMenus (void)
  106. {
  107. WindowPtr    frontWind;
  108. int            theKind;
  109. int            nLines;
  110. int            curLine;
  111. Boolean        drawBar = false;
  112. Str255        eString;
  113. StringPtr    fStr,
  114.             eStr = eString,        /* initial setting, may change */
  115.             oStr;
  116.  
  117.     curLine = mapList->curLine;
  118.     nLines = mapList->nLines;
  119.     frontWind = FrontWindow ();
  120.     theKind = ((WindowPeek) frontWind)->windowKind;
  121.  
  122.     if (frontWind == mapWind)
  123.     {
  124.         fStr = (StringPtr) "\p111111001101";
  125.         oStr = (StringPtr) "\p1111010110101";
  126.         CopyString ("\p11011110110111", eStr);    /* points to eString */
  127.  
  128.         if (undoOp == noUndo)
  129.             eStr[undo + 1] = '0';
  130.  
  131.         if (curLine == noLine)
  132.         {
  133.             eStr[cut + 1] = '0';
  134.             eStr[copy + 1] = '0';
  135.             eStr[clear + 1] = '0';
  136.             eStr[duplicate + 1] = '0';
  137.         }
  138.  
  139.         if (haveClipSpec == false)
  140.             eStr[paste + 1] = '0';
  141.  
  142.         if (nLines < 2)
  143.         {
  144.             eStr[sort + 1] = '0';
  145.             eStr[squish + 1] = '0';
  146.             if (nLines == 0)
  147.                 eStr[reverse + 1] = '0';
  148.         }
  149.         else if (nLines >= mapList->maxLines)
  150.         {
  151.             if (curLine == noLine)
  152.                 eStr[paste + 1] = '0';
  153.             eStr[new + 1] = '0';
  154.             eStr[duplicate + 1] = '0';
  155.         }
  156.     }
  157.     else
  158.     {
  159.         /*
  160.          * Settings for File and Options menus are same for DA's and display
  161.          * windows
  162.          */
  163.         fStr = (StringPtr) "\p100000100001";
  164.         oStr = (StringPtr) "\p1100010000101";
  165.  
  166.         if (theKind < 0)                /* DA in front */
  167.             eStr = (StringPtr) "\p11011110000000";
  168.         else                            /* some display window in front */
  169.             eStr = (StringPtr) "\p0";
  170.     }
  171.  
  172.     drawBar = SetMenuStatus (fileMenu, fStr);
  173.     drawBar |= SetMenuStatus (editMenu, eStr);
  174.     drawBar |= SetMenuStatus (optionsMenu, oStr);
  175.  
  176.     if (drawBar)
  177.         DrawMenuBar ();
  178.     
  179. }
  180.  
  181.  
  182. /*
  183.  * Handle "About FaceLift..." selection from Apple menu.
  184.  */
  185.  
  186. static pascal void
  187. DoAppleMenu (short item)
  188. {
  189.     (void) SkelAlert (aboutAlrtNum, SkelDlogFilter (nil, true),
  190.                                                 skelPositionOnParentDevice);
  191.     SkelRmveDlogFilter ();
  192. }
  193.  
  194.  
  195. /*
  196.  * Process selection from File menu
  197.  */
  198.  
  199. static pascal void
  200. DoFileMenu (short item)
  201. {
  202.     switch (item)
  203.     {
  204.  
  205.     case newMap:
  206.         if (DiscardChanges ())
  207.         {
  208.             ClobberMap ();
  209.             ClearMapName ();
  210.             undoOp = noUndo;
  211.             mapModified = false;
  212.         }
  213.         break;
  214.  
  215.     case openMap:
  216.         if (DiscardChanges ())
  217.         {
  218.             if (OpenMap ())
  219.             {
  220.                 undoOp = noUndo;
  221.                 mapModified = false;
  222.             }
  223.         }
  224.         break;
  225.  
  226.     case appendMap:
  227.         if (AddMap ())
  228.         {
  229.             undoOp = noUndo;
  230.             mapModified = true;
  231.         }
  232.         break;
  233.  
  234.     case saveMap:
  235.         if (SaveMap (false))
  236.             mapModified = false;
  237.         break;
  238.  
  239.     case saveMapAs:
  240.         if (SaveMap (true))
  241.             mapModified = false;
  242.         break;
  243.     
  244.     case close:
  245.         SkelClose (FrontWindow ());
  246.         break;                                    /* dispose of it */
  247.     
  248.     case reformat:
  249.         Reformat (true);        /* reformat in place */
  250.         break;
  251.     
  252.     case reformatAs:
  253.         Reformat (false);        /* reformat to another document */
  254.         break;
  255.     
  256.     case quit:
  257.         if (DiscardChanges ())
  258.             SkelStopEventLoop ();
  259.         break;
  260.     }
  261.     FixMenus ();
  262. }
  263.  
  264.  
  265. static void
  266. DoUndo (void)
  267. {
  268. MapSpec    tempSpec;
  269.  
  270.     switch (undoOp)
  271.     {
  272.  
  273.     case undoInsert:
  274.         undoOp = undoDelete;
  275.         undoSpec = mapSpec[undoPos];
  276.         DeleteMapping (undoPos);
  277.         break;
  278.  
  279.     case undoDelete:
  280.         undoOp = undoInsert;
  281.         InsertMapping (&undoSpec, undoPos);
  282.         break;
  283.  
  284.     case undoPaste:
  285.         tempSpec = mapSpec[undoPos];
  286.         PasteMapping (&undoSpec, undoPos);
  287.         undoSpec = tempSpec;
  288.         break;
  289.  
  290.     case undoReverse:
  291.         ReverseMap ();
  292.         break;
  293.  
  294.     case undoFieldChg:
  295.         SetMapFieldValue (undoFieldType, undoVal);
  296.         break;
  297.  
  298.     }
  299. }
  300.  
  301.  
  302. /*
  303.  * Process selection from Edit menu
  304.  */
  305.  
  306. static pascal void
  307. DoEditMenu (short item)
  308. {
  309. int    curLine;
  310.  
  311.     if (SystemEdit (item - 1))
  312.         return;
  313.  
  314.     curLine = mapList->curLine;
  315.     switch (item)
  316.     {
  317.     
  318.     case undo:
  319.         DoUndo ();
  320.         break;
  321.     
  322.     case cut:
  323.         clipMSpec = mapSpec[curLine];
  324.         undoSpec = clipMSpec;
  325.         undoOp = undoDelete;
  326.         undoPos = curLine;
  327.         haveClipSpec = true;
  328.         DeleteMapping (curLine);
  329.         break;
  330.     
  331.     case copy:
  332.         clipMSpec = mapSpec[curLine];
  333.         haveClipSpec = true;
  334.         break;
  335.     
  336.     case paste:
  337.         if (curLine != noLine)
  338.         {
  339.             undoSpec = mapSpec [curLine];
  340.             undoOp = undoPaste;
  341.             undoPos = curLine;
  342.             PasteMapping (&clipMSpec, curLine);
  343.         }
  344.         else                                        /* no selection; */
  345.         {                                            /* add at end */
  346.             undoOp = undoInsert;
  347.             undoPos = mapList->nLines;
  348.             if (InsertMapping (&clipMSpec, mapList->nLines) == false)
  349.                 undoOp = noUndo;
  350.         }
  351.         break;
  352.     
  353.     case clear:
  354.         undoSpec = mapSpec[curLine];
  355.         undoOp = undoDelete;
  356.         undoPos = curLine;
  357.         DeleteMapping (curLine);
  358.         break;
  359.     
  360.     case new:
  361.         NewMapping ();
  362.         undoOp = undoInsert;
  363.         break;
  364.     
  365.     case duplicate:
  366.         DupMapping (curLine);    /* changes mapList->curLine */
  367.         undoOp = undoInsert;
  368.         undoPos = mapList->curLine;
  369.         break;
  370.  
  371.     case sort:
  372.         SortMap ();
  373.         undoOp = noUndo;    /* can't undo this */
  374.         break;
  375.  
  376.     case squish:
  377.         SquishMap ();
  378.         undoOp = noUndo;    /* can't undo this */
  379.         break;
  380.  
  381.     case reverse:
  382.         ReverseMap ();
  383.         undoOp = undoReverse;
  384.         break;
  385.  
  386.     }
  387.  
  388.     if (item != copy)
  389.         mapModified = true;
  390.     FixMenus ();
  391. }
  392.  
  393.  
  394. static pascal void
  395. DoOptionsMenu (short item)
  396. {
  397.     switch (item)
  398.     {
  399.  
  400.     case showFormats:
  401.         ShowFormats ();
  402.         break;
  403.  
  404.     case useFormats:
  405.         if (DestroyWarn ()
  406.             && AddFormats ("\pUse", true))    /* add formats, clobber map first */
  407.         {
  408.             undoOp = noUndo;
  409.             mapModified = false;
  410.         }
  411.         break;
  412.  
  413.     case addFormats:
  414.         if (AddFormats ("\pAdd", false))    /* add formats, don't clobber map first */
  415.         {
  416.             undoOp = noUndo;
  417.             mapModified = true;
  418.         }
  419.         break;
  420.  
  421.     case showBad:
  422.         showBadFormats = !showBadFormats;
  423.         CheckItem (optionsMenu, showBad, showBadFormats);
  424.         break;
  425.  
  426.     case stdFontList:
  427.         StrFonts (true);
  428.         SetSelectors (mapList->curLine);
  429.         break;
  430.  
  431.     case systemFontList:
  432.         ResourceFonts (true);
  433.         SetSelectors (mapList->curLine);
  434.         break;
  435.  
  436.     case showFont:
  437.         showCurFont = !showCurFont;
  438.         CheckItem (optionsMenu, showFont, showCurFont);
  439.         SetSelectors (mapList->curLine);
  440.         break;
  441.  
  442.     case getInfo:
  443.         HelpWindow ();
  444.         break;
  445.  
  446.     }
  447.     FixMenus ();
  448. }
  449.  
  450.  
  451. /*
  452.  * Menu initialization.  This must be done *after* the Map window
  453.  * is initialized, because it affects stuff in that window.
  454.  */
  455.  
  456. void
  457. SetupMenus (void)
  458. {
  459.     SkelApple ("\pAbout FaceLift\311", DoAppleMenu);    /* \311 = ellipsis */
  460.  
  461.     fileMenu = GetMenu (fileMenuNum);
  462.     (void) SkelMenu (fileMenu, DoFileMenu, nil, false, false);
  463.  
  464.     editMenu = GetMenu (editMenuNum);
  465.     (void) SkelMenu (editMenu, DoEditMenu, nil, false, false);
  466.  
  467.     optionsMenu = GetMenu (optionsMenuNum);
  468.     (void) SkelMenu (optionsMenu, DoOptionsMenu, nil, false, false);
  469.  
  470.     DrawMenuBar ();
  471.  
  472.     showBadFormats = false;
  473.     DoOptionsMenu (showBad);
  474.     showCurFont = false;
  475.     DoOptionsMenu (showFont);
  476. }
  477.